package crawler.cnki.service.impl;

import crawler.cnki.entity.Paper;
import crawler.cnki.service.PaperService;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PaperImpl implements PaperService {
    @Resource
    private MongoTemplate mongoTemplate;

    @Override
    public void save(Paper paper) {
//        if(findByTitleAndAuth(paper).size()==0)
            mongoTemplate.save(paper);
    }

    @Override
    public Paper findById(String id) {
        Query query = new Query(Criteria.where("_id").is(id));
        return mongoTemplate.find(query, Paper.class).get(0);
    }

    @Override
    public List<Paper> findByTitleAndAuth(Paper paper) {
        Query query = new Query(Criteria.where("title").is(paper.getTitle()).and("auth").is(paper.getAuthor()));
        return mongoTemplate.find(query, Paper.class);
    }
}
